$(function () {
    var winWidth = $(window).width();
    //Initiat WOW JS
    new WOW({animateClass: 'animated'}).init();
    //smoothScroll
    smoothScroll.init();

    var currentPage = 1;
    var pageSize = 5;

    //首页轮播
    var hText = ['国内', '美国', '泰国', '马来西亚', '俄罗斯','格鲁吉亚'];
    var locId = [3,0,1,2,4,5]
    var pText = ['国内医疗机构', '美国医疗机构', '泰国医疗机构', '马来西亚医疗机构', '俄罗斯医疗机构','格鲁吉亚医疗机构'];
    var imgSrc=[ linkPath + '/images/mechanism/china_icon.png',linkPath + '/images/mechanism/america_icon.png',linkPath + '/images/mechanism/thailand_icon.png',linkPath + '/images/mechanism/malaysia_icon.png',linkPath + '/images/mechanism/russia_icon.png',linkPath + '/images/mechanism/gelujiya_icon.png'];
    var imgSrc_bj=[ linkPath + '/images/mechanism/china_bj.jpg',linkPath + '/images/mechanism/america_bj.jpg',linkPath + '/images/mechanism/thailand_bj.jpg',linkPath + '/images/mechanism/malaysia_bj.jpg',linkPath + '/images/mechanism/russia_bj.jpg',linkPath + '/images/mechanism/gelujiya_bj.jpg'];
    var swiper = new Swiper('.banner-swiper-container', {
        pagination: {
            el: '.banner-swiper-pagination',
            clickable: false,   //取消点击切换，改为手动控制
            renderBullet: function (index, className) {
                return '<div class="swiper-pagination-customs ' + className +
                    '" ><div class="banner-left hidden-xs"><img src="'+imgSrc[index]+'" alt=""></div><div class="banner-right">' +
                    '<h6 class="banner-title text-center">'+ hText[index] +'</h6>' +
                    '<p class="banner-p hidden-sm hidden-xs">'+ pText[index] +'</p></div></div>';
            },
        },
        on:{
          slideChange:function () {
              $('#resText').html('');  //切换的时候清空
              changePage(currentPage,pageSize,this.activeIndex);
          }
        },
        // loop:true,
        autoplay:false,
        lazy: {
            loadPrevNext: false,
        }
    });

    function  isIe() {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
//            var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
        var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;

        if(isIE || isIE11){
            return true;
        }
        return false;
    }

    if(isIe()){
        var hStr = $('.swiper-slide img').height() + 'px';
        $("#mechanism").css('height',hStr);
        $("#mechanism .swiper-wrapper").css('height',hStr);
    }


    $(document).on('click', '.swiper-pagination-customs' ,function () {
        var $this = $(this);
        if($this.hasClass('swiper-pagination-bullet-active')) return ;  //终止
        var $thisIndex = $this.index();

        $this.addClass("nodrop");   //添加限制类
        $(".swiper-slide").eq($thisIndex).addClass("swiper-no-swiping"); //禁止滑动
        $(".tips").removeClass("hide"); //显示提示

        $('#resText').html('');  //点击的时候才清空
        currentPage = 1; //在切换swiper 之前 就要 重置当前页
        swiper.slideTo($thisIndex); //手动控制
    });

    function changePage(cPage,pageSize,$thisIndex) {
        var loc = locId[$thisIndex];
        $.when(getHospitalData(cPage,loc,pageSize)).done(function (res) {
            var str = getAllStr(res);
            $('#resText').append(str);


            //替换背景两张图
            var $left_bg =  $('#mechanism .back-left');
            var $right_bg =  $('#mechanism .back-right');
            var temp_right_Idx,temp_left_Idx;
            if($thisIndex == 5){
                temp_left_Idx = $thisIndex - 1;
                temp_right_Idx = 0;
            }else if($thisIndex == 0){
                temp_left_Idx = 5;
                temp_right_Idx = $thisIndex + 1;
            }else {
                temp_left_Idx = $thisIndex - 1;
                temp_right_Idx = $thisIndex + 1;
            }

            $right_bg.css('background-image','url('+ imgSrc_bj[temp_right_Idx] +')');
            $left_bg.css('background-image','url('+ imgSrc_bj[temp_left_Idx] +')')


            //解除
            $(".swiper-slide").eq($thisIndex).removeClass("swiper-no-swiping");
            $(".tips").addClass("hide");
            $(".swiper-pagination-customs").eq($thisIndex).removeClass("nodrop");

        }).fail(function (err) {
            console.log(err)
        });
    }

    function addListCont(currentPage,locCode,pageSize) {
        $.when(getHospitalData(currentPage,locCode,pageSize)).done(function (res) {
            var str = getAllStr(res);
            $('#resText').append(str);
        }).fail(function (err) {
            console.log(err)
        });
    }

    //初始加载国内的
    addListCont(currentPage,locId[0],pageSize);

    function getHospitalData(currentPage,locCode,pageSize) {
        var pageSize = pageSize || 5;
        var dtd = $.Deferred();
        $.ajax({
            type: "POST",
            url: goBaseUrl + "/songbao/v1/hospital",
            data: JSON.stringify({ pageSize:pageSize,currentPage:currentPage,locCode:locCode }),
            dataType: "json",
            success: function(data){
                if(data.Code == '200'){
                    if(!!data.Data ){
                        if(data.Data.hosList.length < pageSize){
                            $(".addMore").addClass('hide');
                        }else {
                            $(".addMore").removeClass('hide');
                        }
                    }else {
                        $('#resText').text('没有任何数据')
                        $(".addMore").addClass('hide');
                    }

                    var dataList = data.Data.hosList
                    dtd.resolve(dataList);
                }else{
                    dtd.reject(data.Msg)
                }
            },
            error:function (err) {
                dtd.reject(err)
            }
        });

        return dtd.promise();
    }

    //加载更多
    $(document).on("click",".addMore",function () {
        currentPage ++ ;
        var curIndex = $(".swiper-pagination-bullet-active").index();
        addListCont(currentPage,locId[curIndex],pageSize);
    })


    function getAllStr(dataList) {
        var str = '';
        $.each(dataList, function(i, v){
            str += getItemStr(v)
        });

        return str;
    }

    function getItemStr(comment) {
        return  '<div class="row"> <div class="col-xs-12 newsBox" data-hos="'+comment['id']+'" > <div class="col-xs-4 newsPic">'
            +'<img src="'+comment['hosImgUrl'].split(',')[0]+'" alt=""> </div>'
            +'<div class="col-xs-8 newsText"><div class="col-xs-12"><h3>'+comment['hosName']+'</h3></div>'
            +'<div class="col-xs-12 address">'+comment['serviceCountry']+'</div><div class="col-xs-12">'
            +'<p class="mainText">'+comment['hosShorted'].replace(/##/g,'')+'</p></div>'
            +'<p class="more"> <a class="hvr-bounce-to-right">了解更多 &rarr;</a></p></div></div> </div>'
    }


    $(document).on('click', '.newsBox' ,function () {
        var idx = $(this).data('hos');
        location.href = location.origin + '/hospital?id=' + idx;
    })
});
